package com.movie.ticket.sale.cinema.assembly.cinemaassembly.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.ticket.sale.cinema.assembly.cinemaassembly.info.*;
import com.movie.ticket.sale.moviecommon.model.TCinemaMovie;
import com.movie.ticket.sale.moviecommon.model.TUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CinemaMapper extends BaseMapper {
    @Select("select * from t_user")
    List<TUser> test();


    /**
     * 通过影院id找到相关联的电影票房
     * @param cinemaId
     * @return
     */
    @Select("SELECT t3.`name`,t3.box_office FROM\n" +
            "t_cinema t1 JOIN t_cinema_movie t2 ON t1.id=t2.cinema_id \n" +
            "JOIN t_movie t3 ON t2.movie_id=t3.id WHERE t1.id=#{cinemaId}")
    List<MovieAcountInfo> findMovieNameById(int cinemaId);

//    @Select("SELECT t1.id,t1.trade_nuo,t1.check_code,t3.tel,t4.name,t5.start_time,t1.real_price FROM t_order_movie t1 \n" +
//            "JOIN t_cinema t2 ON t1.movie_id=t2.id \n" +
//            "JOIN t_user t3 ON t1.user_id=t3.id \n" +
//            "JOIN t_auditorium t4 ON t1.auditorium_id=t4.id\n" +
//            "JOIN t_perid t5 ON t1.perid_id=t5.id\n" +
//            "WHERE t1.cinema_id=#{cinemaId}")

    /**
     * 查询订单，条件为订单号
     * @param page
     * @param cinemaId 影院id
     * @param tradeNuo 订单号
     * @return
     */
    @Select("<script>" +
            "SELECT t1.id,t1.trade_nuo,t1.check_code,t3.tel,t4.`name`,t5.start_time,t1.real_price FROM t_order_movie t1 \n" +
            "JOIN t_cinema t2 ON t1.movie_id=t2.id \n" +
            "JOIN t_user t3 ON t1.user_id=t3.id \n" +
            "JOIN t_auditorium t4 ON t1.auditorium_id=t4.id\n" +
            "JOIN t_perid t5 ON t1.perid_id=t5.id\n" +
            "<where>" +
            "t1.cinema_id=#{cinemaId}"+
            "<if test=\"tradeNuo!=''\">" +
            " and t1.trade_nuo=#{tradeNuo}" +
            "</if>" +
            "</where>"+
            "</script>")
    IPage<MovieOrderInfo> findTicketOrder(Page<MovieOrderInfo> page, int cinemaId,String tradeNuo);

    /**
     * 查找vip订单
     * @param page
     * @param cinemaId
     * @param tradeNuo
     * @return
     */
    @Select("<script>" +
            "SELECT \n" +
            "t1.id,t1.trade_nuo,t2.tel,t3.`name`,t4.ticket_discount,t4.tip_discount,t1.price,t1.`month`,t1.create_time \n" +
            "FROM t_order_vip t1\n" +
            "JOIN t_user t2 ON t1.user_id=t2.id\n" +
            "JOIN t_cinema t3 ON t1.cinema_id=t3.id \n" +
            "JOIN t_vip t4 ON t1.vip_id=t4.id"+
            "<where>" +
            "t1.cinema_id=#{cinemaId}"+
            "<if test=\"tradeNuo!=''\">" +
            " and t1.trade_nuo=#{tradeNuo}" +
            "</if>" +
            "</where>"+
            "</script>")
    IPage<VipOrderInfo> findVipOrder(Page<VipOrderInfo> page, int cinemaId,String tradeNuo);



    /**
     * 通过影院id找到影院的状态，0:申请中 1:人工核查中 2:正常运作 -1:停运
     * @param cinemaId
     * @return
     */
    @Select("SELECT cinema_name,state FROM t_cinema WHERE id=#{cinemaId}")
    CinemaState findCinemaStateById(int cinemaId);

    /**
     * 将电影关联到影院
     * @param tCinemaMovie
     */
    @Insert("insert into t_cinema_movie(cinema_id,movie_id,state) values (#{cinemaId},#{movieId},#{state})")
    void pullMovie(TCinemaMovie tCinemaMovie);

    /**
     * 查看平台是否已经存在该电影
     * @param movieId
     * @param cinemaId
     * @return
     */
    @Select("SELECT * FROM t_cinema_movie WHERE movie_id=#{movieId} AND cinema_id=#{cinemaId}")
    List<CinemaMovieInfo> isExist(int movieId,int cinemaId);

    /**
     * 根据电影id找到电影信息
     * @param movieId
     * @return
     */
    @Select("select\n" +
            "t1.id,\n" +
            "t1.name,\n" +
            "t2.file_path,\n" +
            "t1.type,\n" +
            "t1.introduce,\n" +
            "t1.time,\n" +
            "t1.box_office,\n" +
            "t1.movie_grade,\n" +
            "t1.state,\n" +
            "t1.release_date\n" +
            "from t_movie t1 JOIN t_movie_data t2 ON t1.id=t2.movie_id WHERE t2.type=0 AND t1.id=#{movieId}")
    MovieDataInfo findMovieById(int movieId);

    /**
     * 将电影下架
     * @param movieId
     * @param cinemaId
     * @return
     */
    @Update("update t_cinema_movie SET state=0 WHERE movie_id=#{movieId} and cinema_id=#{cinemaId}")
    void soldOutCinemaMovie(int movieId,int cinemaId);

    /**
     * 将电影上架
     * @param movieId
     * @param cinemaId
     * @return
     */
    @Update("update t_cinema_movie SET state=1 WHERE movie_id=#{movieId} and cinema_id=#{cinemaId}")
    void putawayCinemaMovie(int movieId,int cinemaId);


    /**
     * 新增影厅
     * @param auditoriumInfo
     */
    @Insert("insert into t_auditorium (cinema_id,type,seat_count,seat_stream,name,state) " +
            "values (#{cinemaId},#{type},#{seatCount},#{seatStream},#{name},#{state})")
    void addAuditorium(AuditoriumInfo auditoriumInfo);

    /**
     * 根据影厅id找到影厅信息
     * @param id
     * @return
     */
    @Select("select t1.id,t1.type,t1.seat_count,t1.seat_stream,t1.name,t1.state FROM t_auditorium t1 WHERE id=#{id}")
    AuditoriumInfo findAuditoriumById(int id);

    /**
     * 修改影厅信息
     * @param auditoriumInfo
     */
    @Update("UPDATE t_auditorium SET type=#{type},seat_count=#{seatCount},seat_stream=#{seatStream},`name`=#{name},state=#{state}  " +
            "WHERE id=#{id} and cinema_id=#{cinemaId}")
    void updateAuditorium(AuditoriumInfo auditoriumInfo);

    /**
     * 新增会员规则
     * @param vipInfo
     */
    @Insert("INSERT INTO t_vip (cinema_id,price_var,tip_discount,ticket_discount,state) " +
            "values (#{cinemaId},#{priceVar},#{tipDiscount},#{ticketDiscount},#{state})")
    void addVip(VipInfo vipInfo);

    /**
     * 找到影院的上级，如果是院线返回值为0
     * @param cinemaId
     * @return
     */
    @Select("SELECT chain FROM t_cinema WHERE id=#{cinemaId};")
    int findParentCinema(int cinemaId);

    /**
     * 分页查询vip规则
     * @param page
     * @param cinemaId
     * @param state
     * @return
     */
    @Select("<script>" +
            "SELECT id,price_var,tip_discount,ticket_discount,state FROM t_vip " +
            "<where>" +
            "cinema_id=#{cinemaId}"+
            "<if test=\"state!=null\">" +
            " and state=#{state}" +
            "</if>" +
            "</where>"+
            "</script>")
    IPage<VipInfo> findVip(Page<VipInfo> page, int cinemaId, Integer state);

    /**
     * 修改vip规则信息
     * @param vip
     */
    @Update("update t_vip set price_var=#{priceVar},tip_discount=#{tipDiscount},ticket_discount=#{ticketDiscount},state=#{state} " +
            "where id=#{id} and cinema_id=#{cinemaId}")
    void updateVip(VipInfo vip);


    /**
     * 删除会员规则
     * @param id
     */
    @Delete("delete from t_vip where id=#{id}")
    void delVip(int id);

    /**
     * 判断该时间端影厅是否被占用
     * @param cinemaId
     * @return
     */
    @Select("SELECT id,auditorium_id,start_time,end_time FROM t_perid WHERE auditorium_id=#{auditoriumId} and cinema_id=#{cinemaId}")
    List<UserInfo> isUse(int cinemaId,int auditoriumId);

    /**
     * 新增场次表
     * @param peridInfo
     */
    @Insert("INSERT into t_perid (cinema_id,auditorium_id,movie_id,start_time,end_time,price,seat_stram,state) " +
            "VALUES (#{cinemaId},#{auditoriumId},#{movieId},#{startTime},#{endTime},#{price},#{seatStram},#{state})")
    void addPlan(PeridInfo peridInfo);

    /**
     * 分页条件查询
     * @param page
     * @param state
     * @param auditoriumId
     * @param movieId
     * @return
     */
    @Select("<script>" +
            "SELECT id,auditorium_id,movie_id,start_time,end_time,price,seat_stram,state FROM t_perid" +
            "<where>" +
            "cinema_id=#{cinemaId}"+
            "<if test=\"state!=null \">" +
            " and state=#{state}" +
            "</if>" +
            "<if test=\"auditoriumId!=null\">" +
            " and auditorium_id=#{auditoriumId}" +
            "</if>" +
            "<if test=\"movieId!=null\">" +
            " and movie_id=#{movieId}" +
            "</if>" +
            "</where>"+
            "</script>")
    IPage<PeridInfo> findPlan(Page<PeridInfo> page, int cinemaId, Integer state, Integer auditoriumId, Integer movieId);

    /**
     * 修改排片信息
     * @param info
     */
    @Update("update t_perid set " +
            "auditorium_id=#{auditoriumId},start_time=#{startTime},end_time=#{endTime},price=#{price},seat_stram=#{seatStram}" +
            "where id=#{id}")
    void updatePlan(PeridInfo info);


    /**
     * 找到所有未完成的订单
     * @return
     */
    @Select("SELECT id,start_time,end_time,state FROM t_perid WHERE state>0")
    List<PeridStateInfo> findPeridState();

    /**
     * 修改订单的状态
     * @param id
     * @param state
     */
    @Update("UPDATE t_perid SET state=#{state} WHERE id=#{id}")
    void updatePerid(int id,int state);


    /**
     * 根据id删除场次
     * @param id
     * @return
     */
    @Delete("DELETE from t_perid WHERE id=#{id}")
    void delPlan(int id);

    /**
     * 查询年费
     * @param id
     * @return
     */
    @Select("select cinema_name,annual_cost from t_cinema where id=#{id}")
    CinemaInfo annualCost(int id);

    /**
     * 更新影院的信息
     * @param cinemaId
     * @param tel
     * @param name
     * @param address
     * @param service
     */
    @Update("UPDATE t_cinema SET tel=#{tel} ,cinema_name=#{name} ,address=#{address} ,service=#{service} WHERE id=#{cinemaId}")
    void updateCinemaData(int cinemaId,String tel,String name,String address,String service);


    /**
     * 修改影院文件路径表
     * @param cinemaDataInfo
     */
    @Update("UPDATE t_cinema_data SET file_path=#{filePath} WHERE cinema_id=#{cinemaId} AND type=#{type}")
    void updateCinemaFilePath(CinemaDataInfo cinemaDataInfo);

    /**
     * 根据影院id和类型，找到对应的id
     * @param cinemaId
     * @param type
     * @return
     */
    @Select("select id from t_cinema_data where cinema_id=#{cinemaId} AND type=#{type}")
    int findCinemaDataId(int cinemaId,int type);

    /**
     * 关闭影院
     * @param cinemaId
     */
    @Update("update t_cinema set state=-1 where id=#{cinemaId}")
    void closeCinema(int cinemaId);

    /**
     * 用户取票码取票，修改状态
     * @param checkCode
     */
    @Update("UPDATE t_order_movie SET state=6 WHERE  check_code=#{checkCode}")
    void signIn(String checkCode);

    /**
     * 查看取票码是否存在
     * @param checkCode
     * @return
     */
    @Select("SELECT id FROM t_order_movie WHERE check_code=#{checkCode}")
    List<Integer> isSignIn(String checkCode);



//================================auto===================================================================
    /**
     * 找到所有未完成的订单
     * @return
     */
    @Select("SELECT id,perid_id,movie_id,ticket_price FROM t_order_movie WHERE state>0 and state<7")
    List<AutoOrderInfo> findAllOrder();


    /**
     * 将订单的状态修改为已完成
     * @param id
     */
    @Update("UPDATE t_order_movie SET state=#{state} WHERE id=#{id}")
    void finishOrder(int id, int state);

    /**
     * 根据场次id找到场次的状态
     * @param peridId
     * @return
     */
    @Select("SELECT state FROM t_perid WHERE id=#{peridId}")
    int findperidState(int peridId);

    /**
     * 添加票房
     * @param id
     * @param boxOffice
     */
    @Update("UPDATE t_movie SET box_office=box_office+#{boxOffice} WHERE id=#{id}")
    void addBoxOffice(int id,double boxOffice);


}
